let jobaddModule = (() => {
  let
    jobId,
    jobName = $('.inpBox').find('input[type="text"]'),
    jobDesc = $('.inpBox').find('textarea'),
    jobPowerlist = $('.inpBox').find('input[type="checkbox"]'),
    submit = $('.submit');

  // 渲染  
  let fillJobInfo = async function () {
    let res = await axios.get('/job/info', { params: { jobId } });
    if (res.code) { alert('获取信息失败'); return };
    jobName.val(res.data.name);
    jobDesc.val(res.data.desc);
    let powerArr = res.data.power.split('|');
    [].forEach.call(jobPowerlist, item => {
      let
        id = $(item).prop('id')
      // 如果是部门客户的
      if (/^departcustomer$/.test(id)) {
        console.log(powerArr.includes('customermy'))
        powerArr.includes('customermy') ? $(item).prop('checked', true) : false
      }
      // 如果是全部客户的
      if (/^allcustomer$/.test(id)) {
        console.log(777)
        powerArr.includes('customerall') ? $(item).prop('checked', true) : false;
        $('#departcustomer').prop('checked', true);
      }
      // 通用
      powerArr.includes(id) ? $(item).prop('checked', true) : false
    })
  }

  // 绑定提交事件
  let handleSubmit = function () {
    let deliver = async function () {
      let power = [];
      [].forEach.call(jobPowerlist, item => {
        if($(item).prop('checked')) {
          power.push($(item).val())
        }
      });
      power = power.join('|');
      let baseParams = {
        name: jobName.val().trim(),
        desc: jobDesc.val().trim(),
        power
      }
      if (jobId) {
        // 修改提交
        params = baseParams;
        params.jobId = jobId;
        let res = axios.post('/job/update', params)
        !res.code ? (alert('修改职务信息成功'), window.location.href = 'joblist.html') : alert('修改职务信息失败')
      } else {
        // 添加提交
        params = baseParams;
        let res = axios.post('/job/add', params);
        !res.code ? (alert('添加职务信息成功'), window.location.href = 'joblist.html') : alert('添加职务信息失败')
      }
    };
    submit.click(deliver)
  }
  return {
    init() {
      jobId = window.location.href.getUrlEncodedParams().jobId;
      // 编辑信息渲染
      jobId ? fillJobInfo() : null;
      // 提交按钮的事件绑定
      handleSubmit()
    }
  }
})();

jobaddModule.init()